<?php
namespace app\admin\controller;

use app\admin\model\UserRole;
use think\Controller;
use think\Db;
use think\Request;
use app\admin\model\Role as RoleModel;

class Role extends Controller
{

    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'role';

    /**
     * 角色管理视图
     * @return mixed
     */
    public function index() {

        return $this->fetch();
    }

    /**
     * 分页查询
     * @param int $pageNumber
     * @param int $pageSize
     * @param string $searchText
     * @return \think\response\Json
     */
    public function getList($pageNumber = 1, $pageSize = 5, $searchText = '%') {
        $role = new RoleModel();
        // 分页查询
        $r = $role->order('sort')->whereLike('name', '%'.$searchText.'%')->paginate($pageSize, false, ["page"=>$pageNumber]);
        return json($r);
    }

    /**
     * 添加角色
     * @param Request $request
     * @return \think\response\Json
     */
    public function addRole(Request $request) {
        // 获取参数仅包含数组中字段
        $data = $request->only(['name', 'sort', 'description', 'status']);
        // 添加数据
        $result = RoleModel::create($data);
        // 返回数据
        if ($result) {
            return json(['success'=>true, "msg"=>'添加成功']);
        } else {
            return json(['success'=>false, "msg"=>'添加失败']);
        }
    }

    /**
     * 修改角色
     * @param Request $request
     * @return \think\response\Json
     */
    public function updateRole(Request $request) {
        // 获取参数仅包含数组中字段
        $data = $request->only(['id', 'name', 'sort', 'description', 'status']);
        // 更新数据
        $result = RoleModel::update($data);
        // 返回数据
        if ($result) {
            return json(['success'=>true, "msg"=>'修改成功']);
        } else {
            return json(['success'=>false, "msg"=>'修改失败']);
        }
    }

    /**
     * 删除角色
     * @return \think\response\Json
     */
    public function delete() {
        $id = input('id');
        // 删除数据
        //$result = DB::table($this->table)->where("id", $id)->delete();
        $result = RoleModel::destroy($id);
        // 返回数据
        if ($result) {
            return json(['success'=>true, "msg"=>'删除成功']);
        } else {
            return json(['success'=>false, "msg"=>'删除用户不存在']);
        }
    }

    /**
     * 传入角色用户ID 关联表查询关联信息
     * @param $id
     * @return \think\response\Json
     */
    public function getUserRole($id) {
        $userRole = new UserRole();
        $result = $userRole->where('uid', $id)->find();
        return json(['success'=>true, "obj"=>$result]);
    }

    public function modifyUserRole(Request $request) {
        $data = $request->param();
        //事物
        $userRole = new UserRole();
        $userRole->startTrans();
        try {
            //删除关联表数据
            $userRole->where("uid", $data['uid'])->delete();
            $userRole->save($data);
        } catch (Exception $e) {
            $userRole->rollback();
            return json(['success'=>false, "msg"=>'分配失败']);
        }
        $userRole->commit();
        return json(['success'=>true, "msg"=>'分配成功']);
    }

}